package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("AdultertypeController")
public class AdultertypeController extends CmiEmaController {

	@RequestMapping("/listAdultertypes")
	public ModelAndView listAdultertypes(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = adultertypePaginationService.updatePagination(request, pagination);
		Set<Adultertype> adultertypes = adultertypePaginationService.getPaginationAdultertypes(pagination);

		mav.addObject("adultertypes", adultertypes);
		mav.addObject("entityName", "adultertype");
		mav.addObject("pagination", pagination);
		mav.setViewName("adultertype/listAdultertypes.jsp");
		return mav;
	}

	public String listAdultertypes() {
		return "redirect:/listAdultertypes";
	}

	@RequestMapping("/viewAdultertype")
	public ModelAndView viewAdultertype(@RequestParam Integer adultertypeId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("adultertype", adultertypeDAO.findAdultertypeByPrimaryKey(adultertypeId));
		mav.setViewName("adultertype/viewAdultertype.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteAdultertypes")
	public ModelAndView confirmDeleteAdultertypes(
			@RequestParam(value = "deleteAdultertypesIds", required = true) Set<Integer> deleteAdultertypesIds) {
		ModelAndView mav = new ModelAndView();

		Set<Adultertype> deleteAdultertypes = adultertypeDAO.findAdultertypesByIds(deleteAdultertypesIds);
		Set<Adultertype> referencedAdultertypes = adultertypeService.loadReferenceAdultertypes(deleteAdultertypes);
		boolean hasReferenced;
		if (referencedAdultertypes.size() == 0) {
			mav.addObject("deleteEntitys", deleteAdultertypes);
			hasReferenced = false;
		} else {
			deleteAdultertypes.removeAll(referencedAdultertypes);
			mav.addObject("referencedEntitys", referencedAdultertypes);
			mav.addObject("deleteEntitys", deleteAdultertypes);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Adultertype");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listAdultertypes");
		return mav;
	}

	@RequestMapping("/deleteAdultertype")
	public String deleteAdultertype(@RequestParam Integer adultertypeId) {
		Adultertype adultertype = adultertypeDAO.findAdultertypeByPrimaryKey(adultertypeId);
		adultertypeService.deleteAdultertype(adultertype);
		return "forward:/listAdultertypes";
	}

	@RequestMapping("/confirmDeleteAdultertype")
	public ModelAndView confirmDeleteAdultertype(@RequestParam Integer adultertypeId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("adultertype", adultertypeDAO.findAdultertypeByPrimaryKey(adultertypeId));

		mav.setViewName("adultertype/deleteAdultertype.jsp");

		return mav;
	}

	@RequestMapping("/deleteAdultertypes")
	public String deleteAdultertypes(
			@RequestParam(value = "deleteAdultertypesId", required = true) Set<Integer> deleteAdultertypesId) {
		adultertypeService.deleteAdultertypes(deleteAdultertypesId);
		return "forward:/listAdultertypes";
	}

	@RequestMapping("/editAdultertype")
	public ModelAndView editAdultertype(@RequestParam Integer adultertypeId) {

		ModelAndView mav = new ModelAndView();

		Adultertype adultertype = adultertypeDAO.findAdultertypeByPrimaryKey(adultertypeId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("adultertype", adultertype);

		// 把所有的引用adultertype对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(adultertypeService.loadAdultertypeObjectReferencedSet(adultertype));

		// 将编辑页面adultertype添加到mav中
		mav.setViewName("adultertype/editAdultertype.jsp");
		return mav;
	}

	@RequestMapping("/newAdultertype")
	public ModelAndView newAdultertype() {
		ModelAndView mav = new ModelAndView();
		Adultertype adultertype = new Adultertype();

		mav.addObject("adultertype", adultertype);

		// 把所有的引用adultertype对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(adultertypeService.loadAdultertypeObjectReferencedSet(adultertype));

		mav.addObject("newFlag", true);
		mav.setViewName("adultertype/editAdultertype.jsp");

		return mav;
	}

	@RequestMapping("/saveAdultertype")
	public ModelAndView saveAdultertype(@ModelAttribute Adultertype adultertype) {

		ModelAndView mav = new ModelAndView();

		Adultertype existingAdultertype = CmiHibernateProxyUtil
				.deproxy(adultertypeDAO.findAdultertypeByPhysicalPrimaryKey(adultertype.getName()));

		// 新建记录
		if (adultertype.getId() == null) {
			if (adultertype.equals(existingAdultertype)) {

				mav.addAllObjects(adultertypeService.loadAdultertypeClassReferencedSet(adultertype));
				// mav = editContinue(mav, adultertype);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("adultertype/editAdultertype.jsp");
			} else {
				adultertypeService.saveAdultertype(adultertype);
				mav.setViewName("forward:/listAdultertypes");
			}
		}
		// 编辑记录
		else {
			Adultertype originAdultertype = CmiHibernateProxyUtil
					.deproxy(adultertypeDAO.findAdultertypeByPrimaryKey(adultertype.getId()));
			if (!adultertype.equals(originAdultertype) && adultertype.equals(existingAdultertype)) {
				mav.addAllObjects(adultertypeService.loadAdultertypeClassReferencedSet(adultertype));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("adultertype/editAdultertype.jsp");
			} else {
				adultertypeService.saveAdultertype(adultertype);
				mav.setViewName("forward:/listAdultertypes");
			}
		}
		return mav;
	}

	@RequestMapping("/adultertypeController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
